using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using SubVersionOne.Message;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace SubVersionOne.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        public event EventHandler<string> OnClose;
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
            this.Credentials = new VersionOneCredentials();
            this.Settings = LocalStorageProvider.LoadSettings();

            var items = LocalStorageProvider.Load();
            if (items.Any())
            {
                this.Items = new ObservableCollection<SelectableItemViewModel>(items);
            }
            else
            {
                this.Items = new ObservableCollection<SelectableItemViewModel>();
            }

            
        }

        public void CloseDialog( )
        {
            if (this.OnClose != null)
            {
                this.OnClose(this, this.GetMessage());
            }
        }

        private bool _IsBusy = false;
        public bool IsBusy
        {
            get
            {
                return _IsBusy;
            }
            set
            {
                _IsBusy = value;
                this.RaisePropertyChanged("IsBusy");
            }
        }

        private VersionOneCredentials _Credentials;
        public VersionOneCredentials Credentials
        {
            get
            {

                return _Credentials;
            }
            set
            {
                _Credentials = value;
                this.RaisePropertyChanged("Credentials");
            }
        }


        private SettingsViewModel _Settings;

        public SettingsViewModel Settings
        {
            get
            {
                return _Settings;
            }
            set
            {
                _Settings = value;
                this.RaisePropertyChanged("Settings");
            }
        }

        private ObservableCollection<SelectableItemViewModel> _Items;

        public ObservableCollection<SelectableItemViewModel> Items
        {
            get
            {
                return _Items;
            }
            set
            {
                _Items = value;
                this.RaisePropertyChanged("Items");
            }
        }

        public string GetMessage( )
        {
            try
            {
                StringBuilder b = new StringBuilder();
                foreach (var s in Items.Where(x => x.Selected))
                {
                    b.AppendLine(s.ID + "\n" + s.Name);
                    if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeDescription)
                    {
                        b.AppendLine(s.Description);
                    }
                    if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeUrl)
                    {
                        b.AppendLine(s.Url);
                    }
                    foreach (var item in s.Children.Where(x => x.Selected))
                    {
                        b.AppendLine(item.ID + "\n" + item.Name);
                        if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeDescription)
                        {
                            b.AppendLine(item.Description);
                        }
                        if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeUrl)
                        {
                            b.AppendLine(item.Url);
                        }
                    }
                }

                foreach (var s in Items.Where(x => !x.Selected))
                {
                    foreach (var item in s.Children.Where(x => x.Selected))
                    {
                        b.AppendLine(item.ID + "\n" + item.Name);
                        if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeDescription)
                        {
                            b.AppendLine(item.Description);
                        }
                        if (SubVersionOne.ViewModelLocator.MainStatic.Settings.IncludeUrl)
                        {
                            b.AppendLine(item.Url);
                        }
                    }
                }
                return b.ToString();
            }
            catch (Exception ex)
            {
                EventLogger.Error(ex);
                return String.Empty;
            }
        }
    }
}